import * as types from './../mutation-types'
import search from './search.js'

const state={
	cartlist:[]
}

const getters={
	cartProducts:(state)=>{
		const alllist=foreachid(search.state.all)

return state.cartlist.map(({id,quantity})=>{
	const product = alllist.find(p=>p.sid===id)
	return {
		product,
		quantity
	}
})

 // return state.cart.added.map(({id, quantity}) => {
 //    const product = state.products.all.find(p => p.id === id)
 //    return {
 //      title: product.title,
 //      price: product.price,
 //      quantity
 //    }
 //  })

      // return foreachid(search.state.all);
		
		//return state.cartlist;
	}
}
function foreachid(all){
	//console.log(all)
    var allarr=[];
	for(let i of all){
		for(let c of i.firstlist){
			for(let s of c.secondlist){
				allarr.push(s)
			}
		}
		
	}
	
	//console.log(allarr)
	return allarr
}

const actions ={
	addToCart({commit},detailcon){
		commit(types.ADD_TO_CART, detailcon)
	}
}

const mutations={
	[types.ADD_TO_CART](state,id){
		const record=state.cartlist.find(value=>value.sid===id)
		if(!record){
			state.cartlist.push({
				sid:id,
				quantity:1
			})
		}else{
			record.quantity++
		}
	}
}

export default {
	state,
	getters,
	actions,
	mutations
}